<template>
  <div class="edit-container">
    <el-card class="edit-card">
      <div slot="header" class="clearfix">
        <span>编辑资产</span>
      </div>
      <div class="edit-main">
        <!-- 表单: 栅格布局 -->
        <el-form
          :model="form"
          ref="ruleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="软件名称" prop="softwareName">
                <el-input
                  v-model="form.softwareName"
                  placeholder="请输入软件名称"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件编号" prop="softwareNum">
                <el-input
                  v-model="form.softwareNum"
                  placeholder="请输入软件编号"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件类别" prop="softwareCategoryId">
                <el-select
                  v-model="form.softwareCategoryId"
                  placeholder="请选择软件类别"
                >
                  <el-option
                    v-for="category in softwareCategories"
                    :key="category.id"
                    :label="category.name"
                    :value="category.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="公司" prop="company">
                <el-input
                  v-model="form.company"
                  placeholder="请输入公司名称"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件管理员" prop="adminId">
                <el-select
                  v-model="form.adminId"
                  placeholder="请选择软件管理员"
                >
                  <el-option
                    v-for="admin in users"
                    :key="admin.id"
                    :label="admin.name"
                    :value="admin.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件用途" prop="purposes">
                <el-input
                  v-model="form.purposes"
                  placeholder="请输入软件用途"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件使用人" prop="empId">
                <el-select v-model="form.empId" placeholder="请选择软件使用人">
                  <el-option
                    v-for="user in users"
                    :key="user.id"
                    :label="user.name"
                    :value="user.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件来源" prop="source">
                <el-select v-model="form.source" placeholder="请选择软件来源">
                  <el-option label="内部开发" :value="1"></el-option>
                  <el-option label="外部购买" :value="2"></el-option>
                  <el-option label="开源软件" :value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="供应商" prop="supplierId">
                <el-select v-model="form.supplierId" placeholder="请选择供应商">
                  <el-option
                    v-for="supplier in suppliers"
                    :key="supplier.id"
                    :label="supplier.name"
                    :value="supplier.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="购买时间" prop="buyTime">
                <el-date-picker
                  v-model="form.buyTime"
                  type="datetime"
                  placeholder="选择购买时间"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="购买金额" prop="amount">
                <el-input
                  v-model="form.amount"
                 
                  
                  placeholder="请输入购买金额"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件描述" prop="description">
                <el-input
                  type="textarea"
                  v-model="form.description"
                  placeholder="请输入软件描述"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件版本" prop="softwareVersion">
                <el-input
                  v-model="form.softwareVersion"
                  placeholder="请输入软件版本"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="许可证模式" prop="licenseModel">
                <el-select
                  v-model="form.licenseModel"
                  placeholder="请选择许可证模式"
                >
                  <el-option label="单用户" :value="1"></el-option>
                  <el-option label="多用户" :value="2"></el-option>
                  <el-option label="企业版" :value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="软件版权类型" prop="copyrightType">
                <el-select
                  v-model="form.copyrightType"
                  placeholder="请选择版权类型"
                >
                  <el-option label="专有" :value="1"></el-option>
                  <el-option label="共享" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="授权开始日期" prop="authorizationStartDate">
                <el-date-picker
                  v-model="form.authorizationStartDate"
                  type="date"
                  placeholder="选择开始日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="授权截止日期" prop="authorizationDueDate">
                <el-date-picker
                  v-model="form.authorizationDueDate"
                  type="date"
                  placeholder="选择截止日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="授权码" prop="authorizationCode">
                <el-input
                  v-model="form.authorizationCode"
                  placeholder="请输入授权码"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="授权信息" prop="authorizationInfo">
                <el-input
                  type="textarea"
                  v-model="form.authorizationInfo"
                  placeholder="请输入授权信息"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="维保供应商" prop="maintenVendorId">
                <el-select
                  v-model="form.maintenVendorId"
                  placeholder="请选择维保供应商"
                >
                  <el-option
                    v-for="vendor in maintenanceVendors"
                    :key="vendor.id"
                    :label="vendor.name"
                    :value="vendor.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="维保开始日期" prop="maintenStarted">
                <el-date-picker
                  v-model="form.maintenStarted"
                  type="date"
                  placeholder="选择维保开始日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="维保期限 (月)" prop="warranty">
                <el-input
                  v-model="form.warranty"
                 
                  placeholder="请输入维保期限"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="维保状态" prop="maintenStatus">
                <el-select
                  v-model="form.maintenStatus"
                  placeholder="请选择维保状态"
                >
                  <el-option label="有效" :value="1"></el-option>
                  <el-option label="无效" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="入库时间" prop="entryDate">
                <el-date-picker
                  v-model="form.entryDate"
                  type="datetime"
                  placeholder="选择入库时间"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="清洗时间" prop="cleanDate">
                <el-date-picker
                  v-model="form.cleanDate"
                  type="datetime"
                  placeholder="选择清洗时间"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="录入时间" prop="inputDate">
                <el-date-picker
                  v-model="form.inputDate"
                  type="datetime"
                  placeholder="选择录入时间"
                ></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item
                label="软件预计使用期限 (月)"
                prop="estimateUsageTerm"
              >
                <el-input
                  v-model="form.estimateUsageTerm"
                 
                  placeholder="请输入使用期限"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="入库金额" prop="intoAmount">
                <el-input
                  v-model="form.intoAmount"
                 
                  
                  placeholder="请输入入库金额"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="税率" prop="tax">
                <el-input
                  v-model="form.tax"
                 
                  
                  placeholder="请输入税率"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="残值率" prop="residualValueRate">
                <el-input
                  v-model="form.residualValueRate"
                 
                  
                  placeholder="请输入残值率"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="折旧" prop="impairment">
                <el-input
                  v-model="form.impairment"
                 
                  
                  placeholder="请输入折旧"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="折旧方法" prop="depreciatedMethod">
                <el-select
                  v-model="form.depreciatedMethod"
                  placeholder="请选择折旧方法"
                >
                  <el-option label="线性法" :value="1"></el-option>
                  <el-option label="加速法" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="计提方法" prop="accrualMethod">
                <el-select
                  v-model="form.accrualMethod"
                  placeholder="请选择计提方法"
                >
                  <el-option label="定期计提" :value="1"></el-option>
                  <el-option label="不定期计提" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="创建人" prop="creatorId">
                <el-select v-model="form.creatorId" placeholder="请选择创建人">
                  <el-option
                    v-for="creator in users"
                    :key="creator.id"
                    :label="creator.name"
                    :value="creator.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="warning" @click="goBack()">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import maintenVendorApi from "@/api/mainten-vendor";
import { software } from "@/api/software";
import { softwareCategory } from "@/api/softwareCategory";
import supplierApi from "@/api/supplier";
import userService from "@/api/user";


// import moment from "moment";
export default {
  data() {
    return {
      id: this.$route.params.id,
      form: {
        softwareName: "",
        softwareNum: "",
        softwareCategoryId: "",
        company: "",
        adminId: "",
        purposes: "",
        empId: "",
        source: "",
        supplierId: "",
        buyTime: "",
        amount: "",
        description: "",
        softwareVersion: "",
        licenseModel: "",
        copyrightType: "",
        authorizationStartDate: "",
        authorizationDueDate: "",
        authorizationCode: "",
        authorizationInfo: "",
        maintenVendorId: "",
        maintenStarted: "",
        warranty: "",
        maintenStatus: "",
        entryDate: "",
        cleanDate: "",
        inputDate: "",
        estimateUsageTerm: "",
        intoAmount: "",
        tax: "",
        residualValueRate: "",
        impairment: "",
        depreciatedMethod: "",
        accrualMethod: "",
        creatorId: "",
      },
      rules: {
        softwareName: [
          { required: true, message: "请输入软件名称", trigger: "blur" },
        ],
        softwareNum: [
          { required: true, message: "请输入软件编号", trigger: "blur" },
        ],
        softwareCategoryId: [
          { required: true, message: "请选择软件类别", trigger: "change" },
        ],
        company: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        adminId: [
          { required: true, message: "请选择软件管理员", trigger: "change" },
        ],
        purposes: [{ required: true, message: "请输入软件用途", trigger: "blur" }],
        empId: [
          { required: true, message: "请选择软件使用人", trigger: "change" },
        ],
        source: [
          { required: true, message: "请选择软件来源", trigger: "change" },
        ],
        supplierId: [
          { required: true, message: "请选择供应商", trigger: "change" },
        ],
        buyTime: [
          { required: true, message: "请选择购买时间", trigger: "change" },
        ],
        amount: [
          {
            required: true,
            
            message: "请输入购买金额",
            trigger: "blur",
          },
        ],
        softwareVersion: [
          { required: true, message: "请输入软件版本", trigger: "blur" },
        ],
        licenseModel: [
          { required: true, message: "请选择许可证模式", trigger: "change" },
        ],
        copyrightType: [
          { required: true, message: "请选择版权类型", trigger: "change" },
        ],
        authorizationStartDate: [
          { required: true, message: "请选择授权开始日期", trigger: "change" },
        ],
        authorizationDueDate: [
          { required: true, message: "请选择授权截止日期", trigger: "change" },
        ],
        authorizationCode: [
          { required: true, message: "请输入授权码", trigger: "blur" },
        ],
        authorizationInfo: [
          { required: true, message: "请输入授权信息", trigger: "blur" },
        ],
        maintenVendorId: [
          { required: true, message: "请选择维保供应商", trigger: "change" },
        ],
        maintenStarted: [
          { required: true, message: "请选择维保开始日期", trigger: "change" },
        ],
        warranty: [
          {
            required: true,
            
            message: "请输入维保期限",
            trigger: "blur",
          },
        ],
        maintenStatus: [
          { required: true, message: "请选择维保状态", trigger: "change" },
        ],
        entryDate: [
          { required: true, message: "请选择入库时间", trigger: "change" },
        ],
        cleanDate: [
          { required: true, message: "请选择清洗时间", trigger: "change" },
        ],
        inputDate: [
          { required: true, message: "请选择录入时间", trigger: "change" },
        ],
        estimateUsageTerm: [
          {
            required: true,
            
            message: "请输入预计使用期限",
            trigger: "blur",
          },
        ],
        intoAmount: [
          {
            required: true,
            
            message: "请输入入库金额",
            trigger: "blur",
          },
        ],
        tax: [
          {
            required: true,
            
            message: "请输入税率",
            trigger: "blur",
          },
        ],
        residualValueRate: [
          {
            required: true,
            
            message: "请输入残值率",
            trigger: "blur",
          },
        ],
        impairment: [
          {
            required: true,
            
            message: "请输入折旧",
            trigger: "blur",
          },
        ],
        depreciatedMethod: [
          { required: true, message: "请选择折旧方法", trigger: "change" },
        ],
        accrualMethod: [
          { required: true, message: "请选择计提方法", trigger: "change" },
        ],
        creatorId: [
          { required: true, message: "请选择创建人", trigger: "change" },
        ],
      },
      // 数据需要根据实际情况填充
      softwareCategories: [], // 从API获取软件类别
      users: [], // 从API获取用户
      suppliers: [], // 从API获取供应商
      maintenanceVendors: [], // 从API获取维保供应商
      sources: [
        { id: 1, name: "购入" },
        { id: 2, name: "自建" },
        { id: 3, name: "租赁" },
        { id: 4, name: "捐赠" },
        { id: 5, name: "盘盈" },
        { id: 6, name: "其他" },
      ],
      maintenStatuses: [
        { id: 1, name: "正常" },
        { id: 2, name: "异常" },
      ],
      sources: [
        { id: 1, name: "购入" },
        { id: 2, name: "自建" },
        { id: 3, name: "租赁" },
        { id: 4, name: "捐赠" },
        { id: 5, name: "盘盈" },
        { id: 6, name: "其他" },
      ],
    };
  },
  created() {
    this.getSoftware();
    this.getSoftwareCategories();
    this.getUsers();
    this.getSuppliers();
    this.getMaintenanceVendors();
  },
  methods: {
    getSoftware() {
      software.find(this.id).then((res) => {
        this.form = res.data;
      });
    },
    getSoftwareCategories() {
      softwareCategory.findAll().then((response) => {
        this.softwareCategories = response.data;
      });
    },
    getUsers() {
      userService.findAll().then((response) => {
        this.users = response.data;
      });
    },
    getSuppliers() {
      supplierApi.findAll().then((response) => {
        this.suppliers = response.data;
      });
    },
    getMaintenanceVendors() {
      maintenVendorApi.findAll().then((response) => {
        this.maintenanceVendors = response.data;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log("submit!");
          // 提交逻辑，例如发送到后端
          software
            .update(this.form)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.$router.push({
                name: "Software",
              });
            })
            .catch(() => {
              this.$message({
                message: "提交失败",
                type: "danger",
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.clearfix {
  text-align: center;
}

.input-style {
  width: 100%; /* 输入框宽度一致 */
}

/* 使每个表单项的标签及内容居中 */
.el-form-item__content {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
